package com.um.jdy.app.shop.manager;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.um.jdy.app.shop.entity.dto.AddCommentDTO;
import com.um.jdy.app.shop.entity.vo.OrderCommentItemVO;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.shop.code.ShopOrderCode;
import com.um.jdy.common.shop.entity.po.Order;
import com.um.jdy.common.shop.entity.po.OrderComment;
import com.um.jdy.common.shop.entity.po.OrderGoods;
import com.um.jdy.common.shop.enums.CommentEnum;
import com.um.jdy.common.shop.enums.OrderEnum;
import com.um.jdy.common.shop.service.OrderCommentService;
import com.um.jdy.common.shop.service.OrderGoodsService;
import com.um.jdy.common.shop.service.ShopOrderService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.entity.dto.PageQuery;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class ShopOrderCommentManager {

    @Autowired
    ShopOrderService shopOrderService;

    @Autowired
    OrderGoodsService orderGoodsService;

    @Autowired
    MemberUserService memberUserService;

    @Autowired
    OrderCommentService orderCommentService;

    /**
     * 评价商品
     */
    public void comment(AddCommentDTO dto){
        String userId = memberUserService.get().getMemberUserId();

        Order order = shopOrderService.getEntity(new String[]{"shop_order_id", "member_user_id"},
                new String[]{dto.getShopOrderId(), userId});
        if (null == order || !StringUtil.equals(order.getStatus(), OrderEnum.Status.UnComment.name())) {
            throw new ServiceException(ShopOrderCode.CommentFaild);
        }

        List<OrderGoods> orderGoodsList = orderGoodsService.getList("shop_order_id", dto.getShopOrderId());
        if (orderGoodsList.size() != dto.getGoods().size()) {
            throw new ServiceException(ShopOrderCode.CommentFaild);
        }

        // userService.verifyPunish(StringUtil.getTrim(order.get("member_user_id")), UserPunishType.NO_WORDS);

        for (AddCommentDTO.CommentGoodDTO goods : dto.getGoods()) {

            String type = CommentEnum.Type.Good.name();
            if (goods.getStar() <= 1) {
                type = CommentEnum.Type.Bad.name();
            } else if (1 < goods.getStar() && goods.getStar() < 4) {
                type = CommentEnum.Type.Middle.name();
            }

            OrderComment comment = new OrderComment();
            comment.setMemberUserId(userId);
            comment.setShopOrderGoodsId(goods.getShopOrderGoodsId());
            comment.setStar(goods.getStar());
            comment.setType(type);
            comment.setContent(goods.getContent());
            comment.setThumbs(goods.getThumbs());
            comment.setVideoSrc(goods.getVideoSrc());
            if(null != goods.getResType()){
                comment.setResType(goods.getResType().name());
            }
            if(goods.getResType() == CommentEnum.ResType.Video && StrUtil.isNotBlank(goods.getVideoSrc())){
                comment.setVideoCover(goods.getVideoSrc() + "?x-oss-process=video/snapshot,t_1000,m_fast");
            }
            boolean isMedia = (goods.getResType() == CommentEnum.ResType.Image && goods.getThumbs().length() > 2) ||
                    (goods.getResType() == CommentEnum.ResType.Video && StrUtil.isNotBlank(goods.getVideoSrc()));
            comment.setIsImg(YesOrNo.of(isMedia).name());
            comment.setCreateDate(DateUtil.date());
            comment.setStatus(CommentEnum.ReviewStatus.Pending.name());
            orderCommentService.save(comment);

            if(dto.getPushBuyerShow()){
                if(!isMedia){
                    throw new ServiceException(ShopOrderCode.BuyerShowLackMedia);
                }
                OrderGoods orderGoods = orderGoodsService.getEntity(goods.getShopOrderGoodsId());
                //TODO 发布笔记
                // socialNotesService.push(resType,StringUtil.getTrim(orderGoods.get("goods_name")),content,videoSrc,thumbsJson,"","MJX",StringUtil.getTrim(orderGoods.get("shop_goods_id")));
            }
        }

        order.setStatus(OrderEnum.Status.Completed.name());
        shopOrderService.save(order);
    }

    /**
     * 分页查询我的评价
     * @param dto
     * @return
     */
    public Page<OrderCommentItemVO> getMyCommentPage(PageQuery dto){
        Page page = new Page(dto);
        return orderCommentService.getViewModelPageBySqlName(page,
                "get_order_comment_page", "get_order_comment_count",
                OrderCommentItemVO.class, new String[]{memberUserService.get().getMemberUserId(),memberUserService.getComId()});
    }

}
